<?php

namespace app\api\controller\v1;

use addons\litestore\model\Litestoregoodsspec;
use addons\litestore\model\Wxlitestoregoods;
use app\admin\model\litestore\Litestoregoods;
use app\admin\model\litestore\Litestorenews;
use app\admin\model\Shopjob;
use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function config(){

        $url = "https://mall.xfy66.com";
        $this->success("",$url);
    }

    public function config2(){

        $url = "https://mall.xfy66.com";
        $this->success("",$url);
    }


    public function config3(){

        $url = "https://mall.xfy66.com";
        $this->success("",$url);
    }

    public function config4(){

        $url = "https://mall.xfy66.com";
        $this->success("",$url);
    }

    public function config5(){

        $url = "https://mall.xfy66.com";
        $this->success("",$url);
    }


    public function firstIndex(){


        $page = $this->request->param('page');

        $limit = 1000;
        $offset = ($page - 1) * $limit;


        $data = array();
        $litestorenewsModel = new Litestorenews();
        $banners = $litestorenewsModel->where('type',1)->field('id,image,title')->select();
        foreach ($banners as $key=>$val){
            $banners[$key]['image'] = cdnurl($val['image'],true);
        }
        $data['banner'] = $banners;
        $where = array();
        $where['Shopjob.status'] = 1;

        $shopJobModel = new Shopjob();
        $list = $shopJobModel->with(['worktype','shop','area'])
            ->where($where)
            ->order(['createtime' => 'desc'])->limit($offset, $limit)
            ->select();
        foreach ($list as $key=>$val){

            $list[$key]['tag'] = explode(",",$val['tag']);
        }

        $data['list'] = $list;

        $this->success('',$data);
    }

    /**
     * 首页
     *
     */
    public function index()
    {

        $litestoregoods = new Litestoregoods();

        $type = $this->request->param('type');
        if(!$type){
            $type = 'user';
        }

        $bottomList = $litestoregoods->with(['spec', 'category'])
            ->where('is_delete', '=', 0)
            ->where('goods_status', '=', 10)
            ->where('goods_type',$type)
            ->order(['goods_id' => 'desc', 'goods_sort' => 'asc'])
            ->limit(1000)
            ->select();

        foreach ($bottomList as $key=>$val){

            $image = $val['images'];
            $images = explode(",",$image);
            foreach ($images as $k=>$v){
                $images[$k] =  cdnurl($v,true);
            }
            $bottomList[$key]['images'] = $images;
        }
        $this->success('',$bottomList);
    }



    /**
     * 秒杀
     */
    public function miaoshaList()
    {

        $litestoregoods = new Litestoregoods();
        $list = $litestoregoods->with(['spec', 'category'])
            ->where('is_delete', '=', 0)
            ->where('goods_status', '=', 10)->where('goods_type','miao')
            ->order(['goods_id' => 'desc', 'goods_sort' => 'asc'])
            ->limit(100)
            ->select();
        foreach ($list as $key=>$val){

            $image = $val['images'];
            $images = explode(",",$image);
            foreach ($images as $k=>$v){
                $images[$k] =  cdnurl($v,true);
            }
            $list[$key]['images'] = $images;
        }
        $this->success('',$list);
    }


    /**
     * 秒杀
     */
    public function buyList()
    {

        $litestoregoods = new Litestoregoods();
        $list = $litestoregoods->with(['spec', 'category'])
            ->where('is_delete', '=', 0)
            ->where('goods_status', '=', 10)->where('goods_type','buy')
            ->order(['goods_id' => 'desc', 'goods_sort' => 'asc'])
            ->limit(100)
            ->select();
        foreach ($list as $key=>$val){

            $image = $val['images'];
            $images = explode(",",$image);
            foreach ($images as $k=>$v){
                $images[$k] =  cdnurl($v,true);
            }
            $list[$key]['images'] = $images;
        }
        $this->success('',$list);
    }

    public function getCateProductList(){

        $type = $this->request->param('type');
        $id = '-1';
        $filter = [];
        if($type == 'join'){
            $id = 12;
            $filter['category_id'] = $id;
        }else if($type == 'year'){
            //年货
            $id = 10;
            $filter['category_id'] = $id;
        }else if($type == 'recommend'){
            $filter['is_recommend'] = 1;
        }

        $filter['goods_status'] = '10';
        $filter['is_delete'] = '0';
        // 排序规则
        $sort = [];
        // 商品表名称
        $model_temp = new Wxlitestoregoods();
        $tableName = $model_temp->getTable();
        // 多规格商品 最高价与最低价
        $GoodsSpec = new Litestoregoodsspec();
        $minPriceSql = $GoodsSpec->field(['MIN(goods_price)'])
            ->where('goods_id', 'EXP', "= `$tableName`.`goods_id`")->buildSql();
        $maxPriceSql = $GoodsSpec->field(['MAX(goods_price)'])
            ->where('goods_id', 'EXP', "= `$tableName`.`goods_id`")->buildSql();

        $listdata = $model_temp->field(['*', '(sales_initial + sales_actual) as goods_sales',
            "$minPriceSql AS goods_min_price",
            "$maxPriceSql AS goods_max_price"
        ])->with(['spec'])->where($filter)->order($sort)
            ->paginate(80, false, ['page' => 1]);

        foreach ($listdata as $index => $item) {
            $blistdatarList[] = ['image' => cdnurl(explode(",", $item['images'])[0], true), 'goods_name' => $item['goods_name'],
                'id' => $item['goods_id'], 'goods_min_price' => $item['goods_min_price'], 'goods_max_price' => $item['goods_max_price'], 'goods_sales' => $item['goods_sales']];
        }

        foreach ($listdata as $index => $item){

            $image =  cdnurl(explode(",", $item['images'])[0], true);
            $listdata[$index]['image'] = $image;
        }

        $pagedata = $listdata;
        $this->success('', $pagedata);

    }


    public function checkGoodsTime(){

        $litestoreGoods = new \app\admin\model\Litestoregoods();
        $list = $litestoreGoods->where('goods_status',10)->select();
        foreach ($list as $key=>$val){

            $expire = $val['expire'];
            if($expire != -1){
                $time = strtotime($expire);
                if ($time <= time()) {
                    $litestoreGoods->where('goods_id', $val['goods_id'])->update(['goods_status' => 20]);
                }
            }
        }
    }
}
